<?

require_once("cms/core/action/AbstractListAction.php");
require_once("cms/core/om/CategoryPeer.php");
require_once("cms/core/om/CategoryI18nPeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating page_categorys");
        $c = new Criteria();
        $c->add(CategoryI18nPeer::CATEGORY_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(CategoryI18nPeer::ACTIVE, true);
        try {
            CategoryI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating page categorys");
        $c = new Criteria();
        $c->add(CategoryI18nPeer::CATEGORY_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(CategoryI18nPeer::ACTIVE, false);
        try {
            CategoryI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        require_once("cms/core/om/BinaryFilePeer.php");

        $this->log->debug("Deleting page categorys");

        $c = new Criteria();
        $c->add(CategoryI18nPeer::CATEGORY_I18N_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $categoryI18ns = CategoryI18nPeer::doSelectJoinCategory($c);
            foreach($categoryI18ns as $categoryI18n) {

                // Delete the page itself
                if($categoryI18n->getLanguageCode() ==
                        $request->getContext()->getInitParameter(_Core::DEFAULT_LANGUAGE)) {

                    $category = $categoryI18n->getCategory();

                    // Move all higher positions one up.
                    $query =
                    "UPDATE category SET position = position - 1 ".
                    "WHERE module = '" .$category->getModule()."' ".
                    "AND position > " . $category->getPosition();

                    $stmt = $conn->createStatement();
                    $stmt->executeUpdate($query, $conn);

                    // Delete the images.
                    if(!is_null($category->getImageRelatedByThumbnailId()))
                        BinaryFilePeer::doDelete($category->getImageRelatedByThumbnailId()->getBinaryFile(), $conn);

                    if(!is_null($category->getImageRelatedByImageId()))
                        BinaryFilePeer::doDelete($category->getImageRelatedByImageId()->getBinaryFile(), $conn);
                    
                    // Delete the category and have the onDelete=cascade take care of the rest.
                    CategoryPeer::doDelete($category, $conn);

                } else {

                    // Just this row.
                    CategoryI18nPeer::doDelete($categoryI18n, $conn);
                }
            }

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e);
        }

    }

    public function moveup(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "up");
    }

    public function movedown(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "down");
    }

    private function move($form, $direction) {

        $items = $form->getItems();

        $categoryI18n = CategoryI18nPeer::retrieveByPk($items[0]);
        $category = $categoryI18n->getCategory();

        if(is_null($category)) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE, new ActionMessage("Error finding category."));
            return;
        }

        $conn = Propel::getConnection();
        $stmt = $conn->createStatement();

        try {

            $conn->begin();

            $query =
                "UPDATE category SET position = position ".($direction == "down" ? "-" : "+")." 1 ".
                "WHERE ".
                "module = '".$category->getModule()."' ".
                "AND position = ".($direction == "down" ? $category->getPosition() + 1 : $category->getPosition() - 1);

            $stmt->executeUpdate($query, $conn);

            $query =
                "UPDATE category SET position = position ".($direction == "down" ? "+" : "-")." 1 ".
                "WHERE ".
                "module = '".$category->getModule()."' ".
                "AND category_id = ".$category->getCategoryId();

            $stmt->executeUpdate($query, $conn);

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.move", true));
            $this->log->error($e->getMessage());
        }

    }

}
?>